/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.just.dusin.ooii.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Calendar;

/**
 *
 * @author Administrator
 */
public class DayoutDao {
    private Connection conn = null;
    public int size;

    public DayoutDao(Connection conn) {
        this.conn = conn;
    }


    public boolean doCreate(DayoutBean dayoutBean) throws Exception {
            boolean flag = false;
            PreparedStatement pstmt = null;
             String sql = "INSERT INTO [dayout]([orgcode],[year],[month],[day],[dayout],[status])"
                        + " VALUES (?,?,?,?,?,?)";
        try {
                pstmt = this.conn.prepareStatement(sql);

                pstmt.setString(1, dayoutBean.getMineCode());			
                pstmt.setInt(2, dayoutBean.getYear());
                pstmt.setByte(3, dayoutBean.getMonth());
                pstmt.setByte(4, dayoutBean.getDay());                        
                pstmt.setDouble(5, dayoutBean.getDayout());
                pstmt.setByte(6, (byte)1);

                int len = pstmt.executeUpdate();
                if (len > 0) {
                        flag = true;
                }
        } catch (Exception e) {
                throw e;
        } finally {
                try {
                        pstmt.close();
                } catch (Exception e) {
                        throw e;
                }
        }
        return flag;
    }
    
    
   public boolean doCreate(String orgCode ,int year,int month,int day) throws Exception {
            boolean flag = false;
            PreparedStatement pstmt = null;
             String sql = "INSERT INTO [dayout]([orgcode],[year],[month],[day],[dayout],[status])"
                        + " VALUES (?,?,?,?,?,?)";
        try {
                pstmt = this.conn.prepareStatement(sql);

                pstmt.setString(1, orgCode);
                pstmt.setShort(2, (short)year);
                pstmt.setByte(3, (byte)month);
                pstmt.setByte(4, (byte)day);                        
                pstmt.setDouble(5, 0);
                pstmt.setByte(6, (byte)1);
                
                int len = pstmt.executeUpdate();
                if (len > 0) {
                        flag = true;
                }
        } catch (Exception e) {
                throw e;
        } finally {
                try {
                        pstmt.close();
                } catch (Exception e) {
                        throw e;
                }
        }
        return flag;
    }
   
   public boolean addOutput( DataOf5mBean d5m) throws Exception {
            boolean flag = false;
            PreparedStatement pstmt = null;
            String orgCode = d5m.getMineCode();
            int year = d5m.getYear();
            int month = d5m.getMonth();
            int day = d5m.getDay();
            
            String sql = "UPDATE [dayout] SET [dayout]=[dayout]+?,[status]=? WHERE [orgcode]=? and [year]=? and [month]=? and [day]=?";
            try {
                    pstmt = this.conn.prepareStatement(sql);
                    
                    pstmt.setDouble(1, d5m.getSumof5min());
                    pstmt.setByte(2, (byte)1);     
                    pstmt.setString(3, orgCode);			
                    pstmt.setInt(4, year);
                    pstmt.setByte(5, (byte)month);
                    pstmt.setByte(6, (byte)day);            
           
                    int len = pstmt.executeUpdate();
                    if (len > 0) {
                            flag = true;
                    }
            } catch (Exception e) {
                    throw e;
            } finally {
                    try {
                            pstmt.close();
                    } catch (Exception e) {
                            throw e;
                    }
            }
            return flag;
    }
   
    public boolean doUpdateOut(DataOf5mBean d5m) throws Exception {
            boolean flag = false;
            boolean flagCreate=false;
            boolean flagAdd =false;         
            
            
            String orgCode = d5m.getMineCode();
            int year = d5m.getYear();
            int month = d5m.getMonth();
            int day = d5m.getDay();            
            
            if(!isExist(orgCode,year,month,day)){
                flagCreate=doCreate(orgCode,year,month,day);
            }else
                flagAdd=true;
            flagAdd = addOutput(d5m);
            flag = flagCreate&&flagAdd;
            return flag;            
    }
   
    //*更新产量，进行累加
//     public boolean doUpdateOut(DataOf5mBean d5m) throws Exception {
//            boolean flag = false;
//            PreparedStatement pstmt = null;
//            
//            Calendar calendar = Calendar.getInstance();
//            String orgCode = d5m.getMineCode();
//            int year = d5m.getYear();
//            int month = d5m.getMonth();
//            int day = d5m.getDay();
//            
//            
//            if(d5m.getMin()==0||d5m.getMin()==1){     //整点数据归为上个小时
//                calendar.set(d5m.getYear(), d5m.getMonth()-1, d5m.getDay(), d5m.getHour(), d5m.getMin());
//                calendar.add(Calendar.MINUTE, -2);
//                 year = calendar.get(Calendar.YEAR);
//                month = calendar.get(Calendar.MONTH)+1;
//                day = calendar.get(Calendar.DATE);            
//            }
//            
//            
//            if(!isExist(orgCode,year,month,day)){
//                doCreate(orgCode,year,month,day);
//            }
//            
//            String sql = "UPDATE [dayout] SET [dayout]=[dayout]+?,[status]=? WHERE [orgcode]=? and [year]=? and [month]=? and [day]=?";
//            try {
//                    pstmt = this.conn.prepareStatement(sql);
//
//                    pstmt.setString(3, d5m.getMineCode());			
//                    pstmt.setInt(4, year);
//                    pstmt.setByte(5, (byte)month);
//                    pstmt.setByte(6, (byte)day);
//                    
//                    
//                    pstmt.setDouble(1, d5m.getSumof5min());
//                    pstmt.setByte(2, (byte)1);                    
//                    
//
//                    int len = pstmt.executeUpdate();
//                    if (len > 0) {
//                            flag = true;
//                    }
//            } catch (Exception e) {
//                    throw e;
//            } finally {
//                    try {
//                            pstmt.close();
//                    } catch (Exception e) {
//                            throw e;
//                    }
//            }
//            return flag;
//    }
    
    public boolean doDeleteById(long pid) throws Exception {
            boolean flag = false;
            PreparedStatement pstmt = null;
            String sql = "DELETE FROM hourout WHERE id=?";
            try {
                    pstmt = this.conn.prepareStatement(sql);
                    pstmt.setLong(1, pid);
                    int len = pstmt.executeUpdate();
                    if (len > 0) {
                            flag = true;
                    }
            } catch (Exception e) {
                    throw e;
            } finally {
                    try {
                            pstmt.close();
                    } catch (Exception e) {
                            throw e;
                    }
            }
            return flag;
    }
    
    /**
     * 根据机构代码、年、月、日判断是否存在该记录
     * @param orgCode
     * @param year
     * @param month
     * @param day
     * @return
     * @throws Exception 
     */
    public boolean isExist(String orgCode,int year,int month,int day) throws Exception {
        
            PreparedStatement pstmt = null;
            String sql = "select * FROM dayout WHERE   orgCode=? and year=? and month=? and day=?";
            try {
                    pstmt = this.conn.prepareStatement(sql);
                    pstmt.setString(1, orgCode);
                    pstmt.setInt(2, year);
                    pstmt.setInt(3, month);
                    pstmt.setInt(4, day);                   
                    
                    ResultSet rs = pstmt.executeQuery();
                    if(rs.next())
                        return true;
                    else
                        return false;
                    
            } catch (Exception e) {
                    throw e;
            } finally {
                    try {
                            pstmt.close();
                    } catch (Exception e) {
                            throw e;
                    }
            }          
    }
    
    
    /**
     *根据机构代码、年、月、日删除一条记录 
     * @param orgCode
     * @param year
     * @param month
     * @param day
     * @return
     * @throws Exception 
     */
    public boolean doDeleteByOrgTime(String orgCode,int year,int month,int day) throws Exception {
            boolean flag = false;
            PreparedStatement pstmt = null;
            String sql = "DELETE FROM dayout WHERE   orgCode=? and year=? and month=? and day=?";
            try {
                    pstmt = this.conn.prepareStatement(sql);
                    pstmt.setString(1, orgCode);
                    pstmt.setInt(2, year);
                    pstmt.setInt(3, month);
                    pstmt.setInt(4, day);                   
                    
                    int len = pstmt.executeUpdate();
                    if (len > 0) {
                            flag = true;
                    }
            } catch (Exception e) {
                    throw e;
            } finally {
                    try {
                            pstmt.close();
                    } catch (Exception e) {
                            throw e;
                    }
            }
            return flag;
    }
    public boolean doDeleteById(String[] pids) throws Exception {
            for (String pid : pids) {
                long id = Long.parseLong(pid);
                doDeleteById(id);
            }
            return true;
    }
    
    public int getTotalSize() {
            return size;
    }
}
